<!--
 *Author：jxx
 *Date：{Date}
 *Contact：283591387@qq.com
 *业务请在@/extension/mes/mes/ProductNotice.jsx或ProductNotice.vue文件编写
 *新版本支持vue或【表.jsx]文件编写业务,文档见:https://doc.volcore.xyz/docs/view-grid、https://doc.volcore.xyz/docs/web
 -->
<template>
    <view-grid ref="grid" :columns="columns" :detail="detail" :details="details" :editFormFields="editFormFields"
        :editFormOptions="editFormOptions" :searchFormFields="searchFormFields" :searchFormOptions="searchFormOptions"
        :table="table" :extend="extend" :onInit="onInit" :onInited="onInited" :searchBefore="searchBefore"
        :searchAfter="searchAfter" :addBefore="addBefore" :updateBefore="updateBefore" :delBefore="delBefore"
        :auditModelOpenBefore="auditModelOpenBefore" :rowClick="rowClick" :modelOpenBefore="modelOpenBefore"
        :modelOpenAfter="modelOpenAfter">
        <!-- 自定义组件数据槽扩展，更多数据槽slot见文档 -->
        <template #gridHeader>
        </template>
        <template #gridFooter>
            <div class="productAssign">
                <productAssign ref="productAssignRef"></productAssign>
            </div>
        </template>
    </view-grid>
</template>
<script setup lang="jsx">
import productAssign from './ProductAssign.vue'
import extend from "@/extension/mes/mes/ProductNotice.jsx";
import viewOptions from './ProductNotice/options.js'
import { ref, reactive, getCurrentInstance, watch, onMounted } from "vue";
const grid = ref(null);
const { proxy } = getCurrentInstance()
const productAssignRef = ref();
//http请求，proxy.http.post/get
const { table, editFormFields, editFormOptions, searchFormFields, searchFormOptions, columns, detail, details } = reactive(viewOptions())

let gridRef;//对应[表.jsx]文件中this.使用方式一样
//生成对象属性初始化
const onInit = async ($vm) => {
    gridRef = $vm;

    proxy.base.setItem('getNoticeSelectRow', () => {
        return gridRef.getTable(true).getSelected();
    })
    proxy.base.setItem('mainSearch', () => {
        gridRef.search()
    })
}
//生成对象属性初始化后,操作明细表配置用到
const onInited = async () => {
    columns.forEach(x => {
        //开启表头过滤
        x.filterData = true;
    })
    
    gridRef.height = gridRef.height - 310;
    if (gridRef.height < 200) {
        gridRef.height = 200;
    }
    gridRef.buttons.splice(3, 0, {
        name: '派工', //按钮名称
        icon: 'el-icon-document', //按钮图标:组件示例->图标
        //primary、success、warning、error、info、text、danger
        type: 'danger',
        plain: true,
        onClick: () => {

            productAssignAddOpen()
            // createProductAssign()
        }
    })
}
const productAssignAddOpen = (row) => {
    let selectRows = gridRef.getSelectRows();
    if (selectRows[0].AuditStatus == 0 || selectRows[0].AuditStatus == null) {
        proxy.$message.error("该通知未审核！");
        return
    }
    if (selectRows[0].IsDipathc == 0 || selectRows[0].IsDipathc == null) {
        proxy.$message.error("该通知未启用派工！");
        return
    }
    if (selectRows.length == 0) {
        proxy.$message.error("请选择生产通知！");
        return
    }
    if (selectRows[0].WaitAssignQty == 0) {
        proxy.$message.error("该通知待派工数量为0！");
        return
    }
    setTimeout(() => {
        productAssignRef.value.$refs.grid.getFormOption('BillNo').readonly = true
        productAssignRef.value.$refs.grid.getFormOption('BillDate').readonly = true
        productAssignRef.value.$refs.grid.getFormOption('ProductNoticeNo').readonly = true
        productAssignRef.value.$refs.grid.getFormOption('ProductProcessNo').readonly = true
        productAssignRef.value.$refs.grid.getFormOption('ProductProcessName').readonly = true
        productAssignRef.value.$refs.grid.getFormOption('ProductProcessTypeId').readonly = true
        productAssignRef.value.$refs.grid.getFormOption('ReportWorkTypeId').readonly = true
        productAssignRef.value.$refs.grid.getFormOption('PlanQty').readonly = true
        productAssignRef.value.$refs.grid.getFormOption('QualifyQty').readonly = true
        productAssignRef.value.$refs.grid.getFormOption('UnQualifyQty').readonly = true
        productAssignRef.value.$refs.grid.editFormFields.ProductNoticeId = selectRows[0].ProductNoticeId
        productAssignRef.value.$refs.grid.editFormFields.ProductTaskProcessId = selectRows[0].ProductTaskProcessId
        productAssignRef.value.$refs.grid.editFormFields.BillDate = proxy.base.getDate()
        productAssignRef.value.$refs.grid.editFormFields.ProductNoticeNo = selectRows[0].BillNo
        productAssignRef.value.$refs.grid.editFormFields.ProductProcessNo = selectRows[0].ProductProcessNo
        productAssignRef.value.$refs.grid.editFormFields.ProductProcessId = selectRows[0].ProductProcessId
        productAssignRef.value.$refs.grid.editFormFields.ProductProcessName = selectRows[0].ProductProcessName
        productAssignRef.value.$refs.grid.editFormFields.ProductProcessTypeId = selectRows[0].ProductProcessTypeId
        productAssignRef.value.$refs.grid.editFormFields.DepartmentId = selectRows[0].DepartmentId
        productAssignRef.value.$refs.grid.editFormFields.DepartmentName = selectRows[0].DepartmentName
        productAssignRef.value.$refs.grid.editFormFields.GroupId = selectRows[0].GroupId
        productAssignRef.value.$refs.grid.editFormFields.ReportWorkTypeId = selectRows[0].ReportWorkTypeId
        productAssignRef.value.$refs.grid.editFormFields.PlanQty = selectRows[0].PlanQty
        productAssignRef.value.$refs.grid.editFormFields.AssignQty = selectRows[0].WaitAssignQty
        productAssignRef.value.$refs.grid.editFormFields.PriceTypeId = selectRows[0].PriceTypeId
        productAssignRef.value.$refs.grid.editFormFields.WorkHourPrice = selectRows[0].WorkHourPrice
        productAssignRef.value.$refs.grid.editFormFields.WorkQtyPrice = selectRows[0].WorkQtyPrice
        productAssignRef.value.$refs.grid.editFormFields.CompleteStatusId = 0
        

    }, 400);
    productAssignRef.value.$refs.grid.add()

}


const searchBefore = async (param) => {
    //界面查询前,可以给param.wheres添加查询参数
    //返回false，则不会执行查询
    param.wheres.push({ name: "IsDipathc", value: 1 })

    return true;
}
const searchAfter = async (rows, result) => {
    return true;
}
const addBefore = async (formData) => {
    //新建保存前formData为对象，包括明细表，可以给给表单设置值，自己输出看formData的值
    return true;
}
const updateBefore = async (formData) => {
    //编辑保存前formData为对象，包括明细表、删除行的Id
    return true;
}
const delBefore = (ids, rows) => { //查询界面的表删除前 ids为删除的id数组,rows删除的行
    let isDeleted = true
    rows.forEach(row => {
        if (row.AuditStatus != 0 && row.AuditStatus != null) {
            isDeleted = false
            proxy.$message.error(row.BillNo + "已审核不允许删除！");
            return isDeleted;
        }
    });
    return isDeleted;//返回false，界面上不会显示
}
const auditModelOpenBefore = (rows, isAnit) => {//审核前
    //rows:审批选中的行数据
    let selectRows = gridRef.getSelectRows()
    let flag = false
    if (isAnit) {
        selectRows.forEach(row => {
            if (row.AssignQty > 0) {
                return proxy.$message.error(row.BillNo + "已派工，不允许反审核！");
            }
            else {
                flag = true;
            }
        });
    }
    else {
        flag = true;
    }

    return flag;
}
const rowClick = ({ row, column, event }) => {
    //查询界面点击行事件
    // grid.value.toggleRowSelection(row); //单击行时选中当前行;
    grid.value.clearSelection();
    //点击行默认选中
    grid.value.toggleRowSelection(row); //单击行时选中当前行;
    //加载明细表
    productAssignRef.value.$refs.grid.search();
}
const modelOpenBefore = async (row) => {//弹出框打开后方法
    return true;//返回false，不会打开弹出框
}
const modelOpenAfter = (row) => {
    //弹出框打开后方法,设置表单默认值,按钮操作等
    gridRef.setFormReadonly(false);

    if (gridRef.currentAction == 'Add') {
        gridRef.boxButtons.forEach((btn) => {
            if (btn.name == '保存') {
                btn.hidden = false;
                //或者设置只读
                // btn.readonly = true;
            }

        });
    }
    else {
        if (row.AuditStatus != 0 && row.AuditStatus != null) {
            gridRef.setFormReadonly(true);
            gridRef.boxButtons.forEach((btn) => {
                if (btn.name == '保存') {
                    btn.hidden = true;
                    //或者设置只读
                    // btn.readonly = true;
                }

            });
        }
        else {
            gridRef.boxButtons.forEach((btn) => {
                if (btn.name == '保存') {
                    btn.hidden = false;
                    //或者设置只读
                    // btn.readonly = true;
                }
            });

        }
    }
}
//监听表单输入，做实时计算
//watch(() => editFormFields.字段,(newValue, oldValue) => {	})
//对外暴露数据
defineExpose({})
</script>
